package com.ys.controller.web.news;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.dto.NameValueSelectDto;
import com.bo.enume.sys.StatusEnum;
import com.ys.entity.news.NewsItem;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 
 * @author Jason
 *
 */
@Controller
@RequestMapping("/web/news/newsItem")
public class WebNewsItemController {
	
	public static Long MENU_ID = 78L;
	
	/**
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, Long pid){
		
		if (pid == null) {
			return W.error(model, "上级ID不能为空");
		}
		
		List<NameValueSelectDto> statusArr = new ArrayList<NameValueSelectDto>();
		for (StatusEnum e : StatusEnum.values()) {
			statusArr.add(new NameValueSelectDto(e.name, e.value.toString(), e.value == StatusEnum.ON_USED.value));
		}
		
		String pname = "--";
		if (pid.longValue() != 0L) {
			pname = new NewsItem().setId(pid).queryFirst().getName();
		}
		
		ToAddDto dto = new ToAddDto(pid, pname, statusArr);
		model.addAttribute("dto", dto);
		return "news/newsItem/toAdd";
	}
	@Data
	@AllArgsConstructor
	private static class ToAddDto{
		private Long pid; // 上级菜单ID
		private String pname; // 上级菜单名称
		private List<NameValueSelectDto> statusArr; // 状态
	}
	
	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){
		
		if (id == null) {
			return W.error(model, "ID不能为空");
		}
		
		List<NameValueSelectDto> statusArr = new ArrayList<NameValueSelectDto>();
		for (StatusEnum e : StatusEnum.values()) {
			statusArr.add(new NameValueSelectDto(e.name, e.value.toString(), e.value == StatusEnum.ON_USED.value));
		}
		
		NewsItem entity = new NewsItem().setId(id).queryFirst();
		
		String name = entity.getName();
		ToEditDto dto = new ToEditDto(id, name, statusArr);
		model.addAttribute("dto", dto);
		return "news/newsItem/toEdit";
	}
	@Data
	@AllArgsConstructor
	private static class ToEditDto{
		private Long id; // ID
		private String name; // 名称
		private List<NameValueSelectDto> statusArr; // 状态
	}
	
	/*
	 * 调整顺序
	 */
	@GetMapping("/toResort")
	public String toResort(Model model, HttpServletRequest request, HttpServletResponse response,
			Long pid){

		
		if (pid == null) {
			return W.error(model, "上级栏目ID不能为空");
		}
		
		List<NewsItem> listYsMenu = new NewsItem().setPid(pid).setOrderby("A.SortNo asc").queryList();
		
		List<ToResortDto.RowDto> rows = new ArrayList<ToResortDto.RowDto>();
		for(NewsItem entity : listYsMenu) {
			Long id = entity.getId();
			String name = entity.getName();
			rows.add(new ToResortDto.RowDto(id, name));
		}
		
		ToResortDto dto = new ToResortDto(rows);
		model.addAttribute("dto", dto);
		return "news/newsItem/toResort";
	}
	@Data
	@AllArgsConstructor
	public static class ToResortDto{
		private List<RowDto> rows; // 数据行
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private Long id; // 字段ID
			private String name; // 字段名称
		}
	}
}
